<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
<meta name="generator" content="Doxygen 1.8.13"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<title>example/c/j1939_db.c</title>
<link href="tabs.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="dynsections.js"></script>
<link href="navtree.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="resize.js"></script>
<script type="text/javascript" src="navtreedata.js"></script>
<script type="text/javascript" src="navtree.js"></script>
<script type="text/javascript">
  $(document).ready(initResizable);
</script>
<link href="search/search.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="search/searchdata.js"></script>
<script type="text/javascript" src="search/search.js"></script>
<link href="doxygen.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<div id="titlearea">
<table cellspacing="0" cellpadding="0">
 <tbody>
 <tr style="height: 56px;">
  <td id="projectlogo"><img alt="Logo" src="kvaser.gif"/></td>
 </tr>
 </tbody>
</table>
</div>
<!-- end header part -->
<!-- Generated by Doxygen 1.8.13 -->
<script type="text/javascript">
var searchBox = new SearchBox("searchBox", "search",false,'Search');
</script>
<script type="text/javascript" src="menudata.js"></script>
<script type="text/javascript" src="menu.js"></script>
<script type="text/javascript">
$(function() {
  initMenu('',true,false,'search.php','Search');
  $(document).ready(function() { init_search(); });
});
</script>
<div id="main-nav"></div>
</div><!-- top -->
<div id="side-nav" class="ui-resizable side-nav-resizable">
  <div id="nav-tree">
    <div id="nav-tree-contents">
      <div id="nav-sync" class="sync"></div>
    </div>
  </div>
  <div id="splitbar" style="-moz-user-select:none;" 
       class="ui-resizable-handle">
  </div>
</div>
<script type="text/javascript">
$(document).ready(function(){initNavTree('example_2c_2j1939_db_8c-example.html','');});
</script>
<div id="doc-content">
<!-- window showing the filter options -->
<div id="MSearchSelectWindow"
     onmouseover="return searchBox.OnSearchSelectShow()"
     onmouseout="return searchBox.OnSearchSelectHide()"
     onkeydown="return searchBox.OnSearchSelectKey(event)">
</div>

<!-- iframe showing the search results (closed by default) -->
<div id="MSearchResultsWindow">
<iframe src="javascript:void(0)" frameborder="0" 
        name="MSearchResults" id="MSearchResults">
</iframe>
</div>

<div class="header">
  <div class="headertitle">
<div class="title">example/c/j1939_db.c</div>  </div>
</div><!--header-->
<div class="contents">
<div class="fragment"><div class="line"><span class="comment">/*</span></div><div class="line"><span class="comment">** This software is furnished as Redistributable under the Kvaser Software Licence</span></div><div class="line"><span class="comment">** https://www.kvaser.com/canlib-webhelp/page_license_and_copyright.html</span></div><div class="line"><span class="comment">** Description:</span></div><div class="line"><span class="comment">** This is a sample console program that uses kvadblib to create and access CAN</span></div><div class="line"><span class="comment">** databases. It creates a J1939 database file named &#39;my_J1939_db.dbc&#39; in</span></div><div class="line"><span class="comment">** the directory where it is invoked.</span></div><div class="line"><span class="comment">** The program is just intended as an example of how to use kvadblib. It&#39;s not</span></div><div class="line"><span class="comment">** performance optimized in any way. It may, or may not, be useful for you.</span></div><div class="line"><span class="comment">*/</span></div><div class="line"></div><div class="line"><span class="preprocessor">#include &lt;stdio.h&gt;</span></div><div class="line"></div><div class="line"><span class="comment">// kvadblib requires the following #include.</span></div><div class="line"><span class="preprocessor">#include &quot;<a class="code" href="kva_db_lib_8h.html">kvaDbLib.h</a>&quot;</span></div><div class="line"></div><div class="line"><span class="comment">// Forward declarations</span></div><div class="line"><span class="keywordtype">void</span> Check(<span class="keyword">const</span> <span class="keywordtype">char</span>* <span class="keywordtype">id</span>, <a class="code" href="kva_db_lib_8h.html#a2f504ec74e40048f49ed23d630528d57">KvaDbStatus</a> stat);</div><div class="line"><a class="code" href="kva_db_lib_8h.html#a0ab6b252bb2dbf1312bd48208954e67b">KvaDbHnd</a> createJ1939Db(<span class="keywordtype">char</span> *path);</div><div class="line"></div><div class="line"><span class="keywordtype">int</span> main(<span class="keywordtype">int</span> argc, <span class="keywordtype">char</span>* argv[])</div><div class="line">{</div><div class="line">  <a class="code" href="kva_db_lib_8h.html#a0ab6b252bb2dbf1312bd48208954e67b">KvaDbHnd</a> db_handle;</div><div class="line">  <a class="code" href="kva_db_lib_8h.html#a2f504ec74e40048f49ed23d630528d57">KvaDbStatus</a> status;</div><div class="line"></div><div class="line">  <span class="keywordtype">char</span>* path = <span class="stringliteral">&quot;my_J1939_db.dbc&quot;</span>;</div><div class="line">  db_handle = createJ1939Db(path);</div><div class="line"></div><div class="line">  <span class="comment">// Write database to file</span></div><div class="line">  status = <a name="a0"></a><a class="code" href="group__kvadb__database.html#gabf1db33bc6778e2f77ab1b66e8f743e9">kvaDbWriteFile</a>(db_handle, path);</div><div class="line">  Check(<span class="stringliteral">&quot;kvaDbWriteFile&quot;</span>, status);</div><div class="line">}</div><div class="line"></div><div class="line"><span class="keywordtype">void</span> Check(<span class="keyword">const</span> <span class="keywordtype">char</span>* <span class="keywordtype">id</span>, <a class="code" href="kva_db_lib_8h.html#a2f504ec74e40048f49ed23d630528d57">KvaDbStatus</a> stat)</div><div class="line">{</div><div class="line">  <span class="keywordflow">if</span> (stat &lt; 0) {</div><div class="line">    printf(<span class="stringliteral">&quot;%s: failed, stat=%d\n&quot;</span>, <span class="keywordtype">id</span>, (<span class="keywordtype">int</span>)stat);</div><div class="line">  }</div><div class="line">}</div><div class="line"></div><div class="line"><span class="comment">/* Construct database from scratch with a single J1939 message</span></div><div class="line"><span class="comment">*  with necessary J1939 attributes.</span></div><div class="line"><span class="comment">*/</span></div><div class="line"><a class="code" href="kva_db_lib_8h.html#a0ab6b252bb2dbf1312bd48208954e67b">KvaDbHnd</a> createJ1939Db(<span class="keywordtype">char</span>* path) {</div><div class="line">  <a class="code" href="kva_db_lib_8h.html#a2f504ec74e40048f49ed23d630528d57">KvaDbStatus</a> status;</div><div class="line">  <a class="code" href="kva_db_lib_8h.html#a0ab6b252bb2dbf1312bd48208954e67b">KvaDbHnd</a> db_handle;</div><div class="line">  <a class="code" href="kva_db_lib_8h.html#a561c9270625ed9b7a4bc32edaab72227">KvaDbMessageHnd</a> mh;</div><div class="line">  <a class="code" href="kva_db_lib_8h.html#aa41cb518eea5c637f18eda0affa4e011">KvaDbSignalHnd</a> sh;</div><div class="line">  <a class="code" href="kva_db_lib_8h.html#a4be0bd27b01ad1ef6e1117369416e7d6">KvaDbAttributeDefHnd</a> ProtocolType_attr, VFrameFormat_attr;</div><div class="line"></div><div class="line">  <span class="keywordtype">char</span>* VFrameFormat_enum_names[4] = {<span class="stringliteral">&quot;StandardCAN&quot;</span>, <span class="stringliteral">&quot;ExtendedCAN&quot;</span>, <span class="stringliteral">&quot;reserved&quot;</span>, <span class="stringliteral">&quot;J1939PG&quot;</span>};</div><div class="line"></div><div class="line">  <span class="comment">/* ######################################</span></div><div class="line"><span class="comment">            Construct J1939 database</span></div><div class="line"><span class="comment">     ###################################### */</span></div><div class="line"></div><div class="line">  <span class="comment">// Open handle to database</span></div><div class="line">  status = <a name="a1"></a><a class="code" href="group__kvadb__database.html#gacbcd92e8da46f1b756c72d25ab92d240">kvaDbOpen</a>(&amp;db_handle);</div><div class="line">  Check(<span class="stringliteral">&quot;kvaDbOpen&quot;</span>, status);</div><div class="line"></div><div class="line">  <span class="comment">// Associate a new database with database handle</span></div><div class="line">  status = <a name="a2"></a><a class="code" href="group__kvadb__database.html#ga0da72bb21d2664d6443f7b3349ec9df5">kvaDbCreate</a>(db_handle, path, NULL);</div><div class="line">  Check(<span class="stringliteral">&quot;kvaDbCreate&quot;</span>, status);</div><div class="line"></div><div class="line">  <span class="comment">// Add necessary J1939 attributes to database</span></div><div class="line">  status = <a name="a3"></a><a class="code" href="group__kvadb__attributes.html#gabbc2f38c6b7c0f437d1d679be2158d7f">kvaDbAddAttributeDefinition</a>(db_handle, &amp;ProtocolType_attr);</div><div class="line">  Check(<span class="stringliteral">&quot;kvaDbAddAttributeDefinition protocolType&quot;</span>, status);</div><div class="line">  status = <a name="a4"></a><a class="code" href="group__kvadb__attributes.html#ga0dd26a857f42e172fcddbccfc374b645">kvaDbSetAttributeDefinitionType</a>(ProtocolType_attr, <a name="a5"></a><a class="code" href="kva_db_lib_8h.html#a097f44c9d7fcb1cd04d12ff5d0f2a133a6956ea5776610bb31bdf93053001564f">kvaDb_AttributeString</a>);</div><div class="line">  Check(<span class="stringliteral">&quot;kvaDbSetAttributeDefinitionType protocolType&quot;</span>, status);</div><div class="line">  status = <a name="a6"></a><a class="code" href="group__kvadb__attributes.html#ga0cca3a491071a7ac3c67c243a95fcf56">kvaDbSetAttributeDefinitionName</a>(ProtocolType_attr, <span class="stringliteral">&quot;ProtocolType&quot;</span>);</div><div class="line">  Check(<span class="stringliteral">&quot;kvaDbSetAttributeDefinitionName protocolType&quot;</span>, status);</div><div class="line">  status = <a name="a7"></a><a class="code" href="group__kvadb__attributes.html#ga40223b0e02ed37ce5a858a99d41e28b6">kvaDbSetAttributeDefinitionOwner</a>(ProtocolType_attr, <a name="a8"></a><a class="code" href="kva_db_lib_8h.html#a486155dbae03cc008297edad213339d0a8f87e1e84d5abd955a3c4a885350162c">kvaDb_AttributeOwnerDb</a>);</div><div class="line">  Check(<span class="stringliteral">&quot;kvaDbSetAttributeDefinitionOwner protocolType&quot;</span>, status);</div><div class="line">  status = <a name="a9"></a><a class="code" href="group__kvadb__attributes.html#ga4d782c663b425aa0da64d83076f41379">kvaDbSetAttributeDefinitionString</a>(ProtocolType_attr, <span class="stringliteral">&quot;J1939&quot;</span>);</div><div class="line">  Check(<span class="stringliteral">&quot;kvaDbSetAttributeDefinitionString protocolType&quot;</span>, status);</div><div class="line"></div><div class="line">  status = <a class="code" href="group__kvadb__attributes.html#gabbc2f38c6b7c0f437d1d679be2158d7f">kvaDbAddAttributeDefinition</a>(db_handle, &amp;VFrameFormat_attr);</div><div class="line">  Check(<span class="stringliteral">&quot;kvaDbAddAttributeDefinition VFrameFormat&quot;</span>, status);</div><div class="line">  status = <a class="code" href="group__kvadb__attributes.html#ga0dd26a857f42e172fcddbccfc374b645">kvaDbSetAttributeDefinitionType</a>(VFrameFormat_attr, <a name="a10"></a><a class="code" href="kva_db_lib_8h.html#a097f44c9d7fcb1cd04d12ff5d0f2a133a0db2fa06ba2229b370e9b8d35b272fc6">kvaDb_AttributeEnumeration</a>);</div><div class="line">  Check(<span class="stringliteral">&quot;kvaDbSetAttributeDefinitionType VFrameFormat&quot;</span>, status);</div><div class="line">  status = <a class="code" href="group__kvadb__attributes.html#ga0cca3a491071a7ac3c67c243a95fcf56">kvaDbSetAttributeDefinitionName</a>(VFrameFormat_attr, <span class="stringliteral">&quot;VFrameFormat&quot;</span>);</div><div class="line">  Check(<span class="stringliteral">&quot;kvaDbSetAttributeDefinitionName VFrameFormat&quot;</span>, status);</div><div class="line">  status = <a class="code" href="group__kvadb__attributes.html#ga40223b0e02ed37ce5a858a99d41e28b6">kvaDbSetAttributeDefinitionOwner</a>(VFrameFormat_attr, <a name="a11"></a><a class="code" href="kva_db_lib_8h.html#a486155dbae03cc008297edad213339d0a432d47a11effc5dcfaba1ac6ff613855">kvaDb_AttributeOwnerMessage</a>);</div><div class="line">  Check(<span class="stringliteral">&quot;kvaDbSetAttributeDefinitionOwner VFrameFormat&quot;</span>, status);</div><div class="line"></div><div class="line">  <span class="keywordflow">for</span> (<span class="keywordtype">int</span> i = 0; i &lt; 4; i++) {</div><div class="line">    status = <a name="a12"></a><a class="code" href="group__kvadb__attributes.html#ga080348ed3dbc47b8fbb35af4ed1aea23">kvaDbAddAttributeDefinitionEnum</a>(VFrameFormat_attr, VFrameFormat_enum_names[i], i);</div><div class="line">    Check(<span class="stringliteral">&quot;kvaDbAddAttributeDefinitionEnum&quot;</span>, status);</div><div class="line">  }</div><div class="line"></div><div class="line">  <span class="comment">/* ######################################</span></div><div class="line"><span class="comment">        Define a message with J1939 flag</span></div><div class="line"><span class="comment">     ###################################### */</span></div><div class="line"></div><div class="line">  <span class="comment">// Add message to database</span></div><div class="line">  status = <a name="a13"></a><a class="code" href="group__kvadb__messages.html#ga20409dc07a8fa5f38cfd2e54f98e5748">kvaDbAddMsg</a>(db_handle, &amp;mh);</div><div class="line">  Check(<span class="stringliteral">&quot;kvaDbAddMsg&quot;</span>, status);</div><div class="line"></div><div class="line">  status = <a name="a14"></a><a class="code" href="group__kvadb__messages.html#gab0420983b735501899087eef7b127ccd">kvaDbSetMsgName</a>(mh, <span class="stringliteral">&quot;NewTestMsgJ1939&quot;</span>);</div><div class="line">  Check(<span class="stringliteral">&quot;kvaDbSetMsgName&quot;</span>, status);</div><div class="line"></div><div class="line">  status = <a name="a15"></a><a class="code" href="group__kvadb__messages.html#ga4adb841c585acfacdb73c5836a7c5b7d">kvaDbSetMsgDlc</a>(mh, 8);</div><div class="line">  Check(<span class="stringliteral">&quot;kvaDbSetMsgDlc&quot;</span>, status);</div><div class="line"></div><div class="line">  status = <a name="a16"></a><a class="code" href="group__kvadb__messages.html#ga2fc55113d8678dfa58a79172d36954ef">kvaDbSetMsgIdEx</a>(mh, 0xCF00401);</div><div class="line">  Check(<span class="stringliteral">&quot;kvaDbSetMsgId&quot;</span>, status);</div><div class="line"></div><div class="line">  <span class="comment">// Set message flags, note flag argument KVADB_MESSAGE_J1939</span></div><div class="line">  status = <a name="a17"></a><a class="code" href="group__kvadb__messages.html#ga950956a97427f88a3657af3624650fdc">kvaDbSetMsgFlags</a>(mh, <a name="a18"></a><a class="code" href="kva_db_lib_8h.html#a0cb969b5cbf74422ee7064b6b8a355f7">KVADB_MESSAGE_J1939</a>);</div><div class="line">  Check(<span class="stringliteral">&quot;kvaDbSetMsgFlags&quot;</span>, status);</div><div class="line"></div><div class="line">  <span class="comment">/* ######################################</span></div><div class="line"><span class="comment">        Add a signal to the message</span></div><div class="line"><span class="comment">     ###################################### */</span></div><div class="line"></div><div class="line">  <span class="comment">// Add signal to message</span></div><div class="line">  status = <a name="a19"></a><a class="code" href="group__kvadb__signals.html#gabbbad3a12d611acbd969734481fcc48c">kvaDbAddSignal</a>(mh, &amp;sh);</div><div class="line">  Check(<span class="stringliteral">&quot;kvaDbAddSignal&quot;</span>, status);</div><div class="line"></div><div class="line">  status = <a name="a20"></a><a class="code" href="group__kvadb__signals.html#ga43c3ac08f5a5114b1495b073d285df06">kvaDbSetSignalName</a>(sh, <span class="stringliteral">&quot;NewTestSignal&quot;</span>);</div><div class="line">  Check(<span class="stringliteral">&quot;kvaDbSetSignalName&quot;</span>, status);</div><div class="line"></div><div class="line">  status = <a name="a21"></a><a class="code" href="group__kvadb__signals.html#ga124f0345c198a4f2c5a79c1620fba6a7">kvaDbSetSignalComment</a>(sh, <span class="stringliteral">&quot;A test signal.&quot;</span>);</div><div class="line">  Check(<span class="stringliteral">&quot;kvaDbSetSignalComment&quot;</span>, status);</div><div class="line"></div><div class="line">  status = <a name="a22"></a><a class="code" href="group__kvadb__signals.html#gabf8723b14dafc4597cd68d72a0608884">kvaDbSetSignalUnit</a>(sh, <span class="stringliteral">&quot;-&quot;</span>);</div><div class="line">  Check(<span class="stringliteral">&quot;kvaDbSetSignalUnit&quot;</span>, status);</div><div class="line"></div><div class="line">  status = <a name="a23"></a><a class="code" href="group__kvadb__signals.html#ga692bc09e34328197d5061cdb521c6c3f">kvaDbSetSignalEncoding</a>(sh, <a name="a24"></a><a class="code" href="kva_db_lib_8h.html#ad67767865a1ea13724d931f94d6eeee4a986b67715a2013d6e8b60e633220d549">kvaDb_Intel</a>);</div><div class="line">  Check(<span class="stringliteral">&quot;kvaDbSetSignalEncoding&quot;</span>, status);</div><div class="line"></div><div class="line">  status = <a name="a25"></a><a class="code" href="group__kvadb__signals.html#gab9a22491498bd216717e9edf627b694d">kvaDbSetSignalRepresentationType</a>(sh, <a name="a26"></a><a class="code" href="kva_db_lib_8h.html#aca4f6042ed5df050dab2ff4278fc7270af1ca5045071de9e05d80d2c7b2975ae0">kvaDb_Unsigned</a>);</div><div class="line">  Check(<span class="stringliteral">&quot;kvaDbSetSignalRepresentationType&quot;</span>, status);</div><div class="line"></div><div class="line">  status = <a name="a27"></a><a class="code" href="group__kvadb__signals.html#ga734770b9b425260f1f9bb4dc5a886aaf">kvaDbSetSignalValueSize</a>(sh, 0, 8);</div><div class="line">  Check(<span class="stringliteral">&quot;kvaDbSetSignalValueSize&quot;</span>, status);</div><div class="line"></div><div class="line">  status = <a name="a28"></a><a class="code" href="group__kvadb__signals.html#ga3bfdb0480993002989f6f73b84768f9c">kvaDbSetSignalValueLimits</a>(sh, 0, 255);</div><div class="line">  Check(<span class="stringliteral">&quot;kvaDbSetSignalValueLimits&quot;</span>, status);</div><div class="line"></div><div class="line">  <span class="keywordflow">return</span> db_handle;</div><div class="line">}</div></div><!-- fragment --> </div><!-- contents -->
</div><!-- doc-content -->
<!-- HTML footer for doxygen 1.8.13-->
<!-- start footer part -->
<div id="nav-path" class="navpath"><!-- id is needed for treeview function! -->
  <ul>
    <li class="footer"> (canlib 5.38) Tue Feb 15 2022</li>
  </ul>
</div>
</body>
</html>
